package www.bplead.com.controller.db;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by wangyangzi on 2016/10/19.
 */
public class DataProvider extends ContentProvider{
    private static final UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    public static String authority;
    private ContentResolver mContentResolver;
    private DbOpenHelper dbOpenHelper;

    @Override
    public boolean onCreate() {
        Log.v("DB","onCreate() true");
        authority = "www.bplead.com";
        init();
        dbOpenHelper = new DbOpenHelper(getContext());
        mContentResolver = getContext().getContentResolver();
        return false;
    }

    private void init(){
        mUriMatcher.addURI(authority, DbConstant.Tables.TABLE_STUDENT, DbConstant.Indexs.INDEX_STUDENT);
        mUriMatcher.addURI(authority, DbConstant.Tables.TABLE_CLASS, DbConstant.Indexs.INDEX_CLASS);
        mUriMatcher.addURI(authority, DbConstant.Tables.TABLE_COURSE, DbConstant.Indexs.INDEX_COURSE);
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        if(db == null){
            return null;
        }

        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        Cursor cursor = null;

        switch (mUriMatcher.match(uri)){
            case DbConstant.Indexs.INDEX_STUDENT:
                qb.setTables(DbConstant.Tables.TABLE_STUDENT);
                cursor = qb.query(db,projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case DbConstant.Indexs.INDEX_CLASS:
                qb.setTables(DbConstant.Tables.TABLE_CLASS);
                cursor = qb.query(db,projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case DbConstant.Indexs.INDEX_COURSE:
                qb.setTables(DbConstant.Tables.TABLE_COURSE);
                cursor = qb.query(db,projection,selection,selectionArgs,null,null,sortOrder);
                break;
        }

        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db == null){
            return null;
        }
        long id = -1;
        switch (mUriMatcher.match(uri)){
            case DbConstant.Indexs.INDEX_STUDENT:
                id = db.insert(DbConstant.Tables.TABLE_STUDENT,null,values);
                mContentResolver.notifyChange(DbConstant.TableStudent.CONTANT_URI,null);
                return ContentUris.withAppendedId(uri,id);
            case DbConstant.Indexs.INDEX_CLASS:
                id = db.insert(DbConstant.Tables.TABLE_CLASS,null,values);
                mContentResolver.notifyChange(DbConstant.TableClass.CONTANT_URI,null);
                return ContentUris.withAppendedId(uri,id);
            case DbConstant.Indexs.INDEX_COURSE:
                id = db.insert(DbConstant.Tables.TABLE_STUDENT,null,values);
                mContentResolver.notifyChange(DbConstant.TableCourse.CONTANT_URI,null);
                return ContentUris.withAppendedId(uri,id);
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public int bulkInsert(Uri uri, ContentValues[] values) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db == null){
            return 0;
        }
        db.beginTransaction();
        try{
            switch (mUriMatcher.match(uri)){
                case DbConstant.Indexs.INDEX_STUDENT:
                    for (ContentValues cv : values) {
                        db.insert(DbConstant.Tables.TABLE_STUDENT, null, cv);
                    }
                    mContentResolver.notifyChange(DbConstant.TableStudent.CONTANT_URI,null);
                    break;
                case DbConstant.Indexs.INDEX_CLASS:
                    for (ContentValues cv : values) {
                        db.insert(DbConstant.Tables.TABLE_CLASS, null, cv);
                    }
                    mContentResolver.notifyChange(DbConstant.TableClass.CONTANT_URI,null);
                    break;
                case DbConstant.Indexs.INDEX_COURSE:
                    for (ContentValues cv : values) {
                        db.insert(DbConstant.Tables.TABLE_COURSE, null, cv);
                    }
                    mContentResolver.notifyChange(DbConstant.TableCourse.CONTANT_URI,null);
                    break;
            }
            db.setTransactionSuccessful();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            db.endTransaction();
        }
        return values.length;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db == null){
            return 0;
        }

        int count = 0;

        switch (mUriMatcher.match(uri)){
            case DbConstant.Indexs.INDEX_STUDENT:
                count = db.delete(DbConstant.Tables.TABLE_STUDENT,selection,selectionArgs);
                break;
            case DbConstant.Indexs.INDEX_CLASS:
                count = db.delete(DbConstant.Tables.TABLE_CLASS,selection,selectionArgs);
                break;
            case DbConstant.Indexs.INDEX_COURSE:
                count = db.delete(DbConstant.Tables.TABLE_COURSE,selection,selectionArgs);
                break;
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        if(db == null){
            return 0;
        }

        int count = 0;

        switch (mUriMatcher.match(uri)){
            case DbConstant.Indexs.INDEX_STUDENT:
                count = db.update(DbConstant.Tables.TABLE_STUDENT,values,selection,selectionArgs);
                break;
            case DbConstant.Indexs.INDEX_CLASS:
                count = db.update(DbConstant.Tables.TABLE_CLASS,values,selection,selectionArgs);
                break;
            case DbConstant.Indexs.INDEX_COURSE:
                count = db.update(DbConstant.Tables.TABLE_COURSE,values,selection,selectionArgs);
                break;
        }
        return count;
    }
}
